/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tcardrecordinfo.service;

import java.util.List;
import java.util.Map;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.mob.common.DataConvertHelper;
import com.jeeplus.modules.tbuildinginfo.entity.TBuildingInfo;
import com.jeeplus.modules.tbuildinginfo.mapper.TBuildingInfoMapper;
import com.jeeplus.modules.twaycardrecordinfo.mapper.TWaycardrecordInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.tcardrecordinfo.entity.TCardcecordInfo;
import com.jeeplus.modules.tcardrecordinfo.mapper.TCardcecordInfoMapper;

import javax.servlet.http.HttpServletRequest;

/**
 * 打卡记录Service
 * @author pr
 * @version 2022-04-22
 */
@Service
@Transactional(readOnly = true)
public class TCardcecordInfoService extends CrudService<TCardcecordInfoMapper, TCardcecordInfo> {

	@Autowired
	private TCardcecordInfoMapper tCardcecordInfoMapper;

	@Autowired
	private TWaycardrecordInfoMapper tWaycardrecordInfoMapper;

	@Autowired
	private TBuildingInfoMapper tBuildingInfoMapper;

	public TCardcecordInfo get(String id) {
		return super.get(id);
	}
	
	public List<TCardcecordInfo> findList(TCardcecordInfo tCardcecordInfo) {
		return super.findList(tCardcecordInfo);
	}
	
	public Page<TCardcecordInfo> findPage(Page<TCardcecordInfo> page, TCardcecordInfo tCardcecordInfo) {
		return super.findPage(page, tCardcecordInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(TCardcecordInfo tCardcecordInfo) {
		super.save(tCardcecordInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(TCardcecordInfo tCardcecordInfo) {
		super.delete(tCardcecordInfo);
	}

	// 审核
	@Transactional(readOnly = false)
	public AjaxJson audit(String id, String type, String status) {
		int rows = 0;
		int wayRows = 0;
		int buildingRows = 0;  //
		int audit = tCardcecordInfoMapper.audit(id, type, status);
		TCardcecordInfo tCardcecordInfo = tCardcecordInfoMapper.get(id);
		if (DataConvertHelper.isNotEmpty(status) && status.equals("1")) {  // 通过
			if (audit > 0) {
				if (DataConvertHelper.isNotEmpty(type) && type.equals("1")) {  // 在途打卡
					wayRows = tWaycardrecordInfoMapper.auditWayCard(Integer.parseInt(tCardcecordInfo.getWaycardrecordId()),status);
				}

				if (DataConvertHelper.isNotEmpty(type) && type.equals("2")) {   // 楼宇打卡
					buildingRows = tWaycardrecordInfoMapper.auditBuildCard(Integer.parseInt(tCardcecordInfo.getBuildingcardrecordId()),status);
					rows = tBuildingInfoMapper.addVisitCount(tCardcecordInfo.getBuildingId());// 增加拜访次数
				}
			}
			if ((wayRows > 0 || (buildingRows > 0 && rows > 0))) {
				return AjaxJson.success("通过审核！");
			} else {
				return AjaxJson.success("审核失败，请重试！");
			}
		}

		if (DataConvertHelper.isNotEmpty(status) && status.equals("2")) {  // 拒绝
			if (audit > 0) {
				if (DataConvertHelper.isNotEmpty(type) && type.equals("1")) {  // 在途打卡
					wayRows = tWaycardrecordInfoMapper.auditWayCard(Integer.parseInt(tCardcecordInfo.getWaycardrecordId()),status);
				}

				if (DataConvertHelper.isNotEmpty(type) && type.equals("2")) {   // 楼宇打卡
					buildingRows = tWaycardrecordInfoMapper.auditBuildCard(Integer.parseInt(tCardcecordInfo.getBuildingcardrecordId()),status);
				}
			}
			if (wayRows > 0 || buildingRows > 0) {
				return AjaxJson.success("通过审核！");
			} else {
				return AjaxJson.success("审核失败，请重试！");
			}
		}
		return AjaxJson.error("错误审批，请重试！");
	}

	// 今日总打卡数
	public AjaxJson cardCount() {
		int count = tCardcecordInfoMapper.cardCount();
		return AjaxJson.success().put("data",count);
	}

	// 今日在途打卡总次数
	public AjaxJson wayCardCount(String punchType) {
		int count = tCardcecordInfoMapper.wayCardCount(punchType);
		return AjaxJson.success().put("data",count);
	}

	// 今日楼宇打卡总次数
	public AjaxJson buildingCardCount(String punchType) {
		int count = tCardcecordInfoMapper.wayCardCount(punchType);
		return AjaxJson.success().put("data",count);
	}

	// 人员打卡定位图列表
	public AjaxJson queryAllMap(HttpServletRequest request) {
		Map<String, Object> params = DataConvertHelper.getRequestParams(request);
		List<Map<String, Object>> list = tCardcecordInfoMapper.queryAllMap(params);
		return AjaxJson.success().put("data",list);
	}
	
}